# Copyright 2023 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

load(
    "//build_tools/bazel:build_defs.oss.bzl",
    "iree_compiler_cc_library",
    "iree_gentbl_cc_library",
    "iree_tablegen_doc",
)

package(
    default_visibility = ["//visibility:public"],
    features = ["layering_check"],
    licenses = ["notice"],  # Apache 2.0
)

iree_gentbl_cc_library(
    name = "PassesIncGen",
    tbl_outs = [
        (
            ["--gen-pass-decls"],
            "Passes.h.inc",
        ),
    ],
    tblgen = "@llvm-project//mlir:mlir-tblgen",
    td_file = "Passes.td",
    deps = ["@llvm-project//mlir:PassBaseTdFiles"],
)

iree_compiler_cc_library(
    name = "PassHeaders",
    hdrs = [
        "Passes.h",
        "Passes.h.inc",
    ],
    deps = [
        ":PassesIncGen",
        "//compiler/src/iree/compiler/Codegen/Dialect/Codegen/IR:IREECodegenDialect",
        "//compiler/src/iree/compiler/Dialect/HAL/IR",
        "//compiler/src/iree/compiler/Utils",
        "@llvm-project//mlir:DialectUtils",
        "@llvm-project//mlir:LinalgTransforms",
        "@llvm-project//mlir:MemRefDialect",
        "@llvm-project//mlir:NVGPUDialect",
        "@llvm-project//mlir:Pass",
        "@llvm-project//mlir:TransformUtils",
        "@llvm-project//mlir:Transforms",
        "@llvm-project//mlir:UBDialect",
    ],
)

iree_compiler_cc_library(
    name = "CommonGPUPasses",
    srcs = [
        "AMDGPUDistributeContract.cpp",
        "DecomposeHorizontallyFusedGemms.cpp",
        "ExpandGPUOps.cpp",
        "GPUAllocPrivateMemoryForDPSOps.cpp",
        "GPUApplyPaddingLevel.cpp",
        "GPUApplyTilingLevel.cpp",
        "GPUBubbleResourceCasts.cpp",
        "GPUCheckResourceUsage.cpp",
        "GPUCombineLayoutTransformation.cpp",
        "GPUCombineValueBarriers.cpp",
        "GPUConvertToCoalescedDMA.cpp",
        "GPUCreateFastSlowPath.cpp",
        "GPUDistribute.cpp",
        "GPUDistributeCopyUsingForall.cpp",
        "GPUDistributeForall.cpp",
        "GPUDistributeScfFor.cpp",
        "GPUDistributeSharedMemoryCopy.cpp",
        "GPUDistributionPatterns.cpp",
        "GPUFuseAndHoistParallelLoops.cpp",
        "GPUGeneralizeNamedOps.cpp",
        "GPUGreedilyDistributeToThreads.cpp",
        "GPUInferMemorySpace.cpp",
        "GPULowerToGlobalLoads.cpp",
        "GPUMultiBuffering.cpp",
        "GPUNestedLayoutDistributionPatterns.cpp",
        "GPUPackToIntrinsics.cpp",
        "GPUPadConvs.cpp",
        "GPUPadOperands.cpp",
        "GPUPatterns.cpp",
        "GPUPipelining.cpp",
        "GPUPromoteMatmulOperands.cpp",
        "GPUReduceBankConflicts.cpp",
        "GPUReuseSharedMemoryAllocs.cpp",
        "GPUTensorAlloc.cpp",
        "GPUTensorTile.cpp",
        "GPUTensorTileToSerialLoops.cpp",
        "GPUTile.cpp",
        "GPUTileAndConvertConvToMatmul.cpp",
        "GPUTileReduction.cpp",
        "GPUVectorAlloc.cpp",
        "GPUVectorDistribution.cpp",
        "GPUVerifyDistribution.cpp",
        "Passes.cpp",
        "VectorReductionToGPU.cpp",
        "WorkgroupReordering.cpp",
    ],
    hdrs = [
        "GPUPatterns.h",
        "GPUVectorDistribution.h",
        "Passes.h",
    ],
    deps = [
        ":PassHeaders",
        ":PassesIncGen",
        "//compiler/src/iree/compiler/Codegen/Common",
        "//compiler/src/iree/compiler/Codegen/Common:VectorLayoutAnalysis",
        "//compiler/src/iree/compiler/Codegen/Dialect/Codegen/IR:IREECodegenDialect",
        "//compiler/src/iree/compiler/Codegen/Dialect/Codegen/Utils",
        "//compiler/src/iree/compiler/Codegen/Dialect/GPU/IR:IREEGPUDialect",
        "//compiler/src/iree/compiler/Codegen/Dialect/GPU/Transforms:GPUTransforms",
        "//compiler/src/iree/compiler/Codegen/Dialect/VectorExt/IR:IREEVectorExtDialect",
        "//compiler/src/iree/compiler/Codegen/Interfaces:PartitionableLoopsInterface",
        "//compiler/src/iree/compiler/Codegen/Transforms",
        "//compiler/src/iree/compiler/Codegen/Utils",
        "//compiler/src/iree/compiler/Codegen/Utils:VectorOpUtils",
        "//compiler/src/iree/compiler/Dialect/HAL/IR",
        "//compiler/src/iree/compiler/Dialect/LinalgExt/IR",
        "//compiler/src/iree/compiler/Dialect/LinalgExt/Transforms",
        "//compiler/src/iree/compiler/Dialect/LinalgExt/Utils",
        "//compiler/src/iree/compiler/Dialect/TensorExt/IR",
        "//compiler/src/iree/compiler/Utils",
        "@llvm-project//llvm:Support",
        "@llvm-project//mlir:AMDGPUDialect",
        "@llvm-project//mlir:AMDGPUUtils",
        "@llvm-project//mlir:AffineDialect",
        "@llvm-project//mlir:AffineTransforms",
        "@llvm-project//mlir:AffineUtils",
        "@llvm-project//mlir:Analysis",
        "@llvm-project//mlir:ArithDialect",
        "@llvm-project//mlir:ArithUtils",
        "@llvm-project//mlir:BufferizationDialect",
        "@llvm-project//mlir:BufferizationInterfaces",
        "@llvm-project//mlir:ControlFlowDialect",
        "@llvm-project//mlir:DestinationStyleOpInterface",
        "@llvm-project//mlir:DialectUtils",
        "@llvm-project//mlir:FuncDialect",
        "@llvm-project//mlir:FunctionInterfaces",
        "@llvm-project//mlir:GPUDialect",
        "@llvm-project//mlir:GPUTransformOps",
        "@llvm-project//mlir:GPUTransforms",
        "@llvm-project//mlir:GPUUtils",
        "@llvm-project//mlir:IR",
        "@llvm-project//mlir:LLVMCommonConversion",
        "@llvm-project//mlir:LinalgDialect",
        "@llvm-project//mlir:LinalgInterfaces",
        "@llvm-project//mlir:LinalgTransforms",
        "@llvm-project//mlir:LinalgUtils",
        "@llvm-project//mlir:LoopLikeInterface",
        "@llvm-project//mlir:MemRefDialect",
        "@llvm-project//mlir:MemRefTransforms",
        "@llvm-project//mlir:MemRefUtils",
        "@llvm-project//mlir:NVGPUDialect",
        "@llvm-project//mlir:Pass",
        "@llvm-project//mlir:Rewrite",
        "@llvm-project//mlir:SCFDialect",
        "@llvm-project//mlir:SCFTransforms",
        "@llvm-project//mlir:SCFUtils",
        "@llvm-project//mlir:SideEffectInterfaces",
        "@llvm-project//mlir:Support",
        "@llvm-project//mlir:TensorDialect",
        "@llvm-project//mlir:TensorTransforms",
        "@llvm-project//mlir:TilingInterface",
        "@llvm-project//mlir:TransformUtils",
        "@llvm-project//mlir:Transforms",
        "@llvm-project//mlir:UBDialect",
        "@llvm-project//mlir:ValueBoundsOpInterface",
        "@llvm-project//mlir:VectorDialect",
        "@llvm-project//mlir:VectorToSCF",
        "@llvm-project//mlir:VectorTransforms",
        "@llvm-project//mlir:VectorUtils",
    ],
)

iree_compiler_cc_library(
    name = "GPUHeuristics",
    srcs = [
        "GPUHeuristics.cpp",
    ],
    hdrs = [
        "GPUHeuristics.h",
    ],
    deps = [
        "//compiler/src/iree/compiler/Codegen/Dialect/GPU/IR:IREEGPUDialect",
        "@llvm-project//llvm:Support",
        "@llvm-project//mlir:IR",
        "@llvm-project//mlir:Support",
    ],
)

iree_tablegen_doc(
    name = "CodegenCommonGPUPassesDocGen",
    category = "Passes",
    tbl_outs = [
        (
            [
                "--gen-pass-doc",
            ],
            "CodegenCommonGPUPasses.md",
        ),
    ],
    tblgen = "@llvm-project//mlir:mlir-tblgen",
    td_file = "Passes.td",
    deps = [
        "@llvm-project//mlir:PassBaseTdFiles",
    ],
)
